#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
long long n,k,cnt=0,ans=1e18,a[1000005],b[1000005],c[1000005],dp[100005][105],sum[100005][105],dp2[1005][1005],sum2[1005][1005];
long long get(long long x,long long y)
{
	return (x>=y?x-y:k);
}
int main()
{
	freopen("minimize.in","r",stdin);
	freopen("minimize.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	memset(dp,0x7f7f7f7f,sizeof(dp));
	memset(sum,0x7f7f7f7f,sizeof(sum));
	memset(dp2,0x7f7f7f7f,sizeof(dp2));
	memset(sum2,0x7f7f7f7f,sizeof(sum2));
	for(long long i=0;i<=100;i++)
	{
		dp[0][i]=0,sum[0][i]=0;
	}
	for(long long i=0;i<=1000;i++)
	{
		dp2[0][i]=0,sum2[0][i]=0;
	}
	cin>>n>>k;
	if(k==0)
	{
		cout<<0<<endl;
		return 0;
	}
	for(long long i=1;i<=n;i++)
	{
		cin>>a[i];
		b[++cnt]=a[i];
	}
	sort(b+1,b+1+cnt);
	cnt=unique(b+1,b+1+cnt)-b-1;
	if(n<=1000)
	{
		for(long long i=1;i<=n;i++)
		{
			for(long long j=0;j<=cnt;j++)
			{
				dp2[i][j]=sum2[i-1][j]+get(b[j],a[i]);
				if(i==n)
				{
					ans=min(ans,dp2[i][j]);
				}
			}
			for(long long j=cnt;j>=0;j--)
			{
				sum2[i][j]=min(sum2[i][j+1],dp2[i][j]);
			}
		}
		cout<<ans<<endl;
		return 0;
	}
	for(long long i=1;i<=n;i++)
	{
		for(long long j=0;j<=100;j++)
		{
			dp[i][j]=sum[i-1][j]+get(j,a[i]);
			if(i==n)
			{
				ans=min(ans,dp[i][j]);
			}
		}
		for(long long j=100;j>=0;j--)
		{
			sum[i][j]=min(sum[i][j+1],dp[i][j]);
		}
	}
	cout<<ans<<endl;	
	return 0;
}
